luaEvent system

4/3/2024 Unity3d

DispatchEvent.事件推送

事件添加监听后,如果不需要了,请调用移除事件,否则会参数意想不到的错误.

After the event is added to the listener, if it is not needed, please call the remove event, otherwise an unexpected error will occur.

functions parameter describe
AddEventListener(eventType,listener, function) 1.string,事件类型

2.object,监听者

3.lua function,回调函数
The event type can be any: string, int, enumeration

Listener: generally used for the current script itself

事件类型可以是任意:字符串、int、枚举都可以

监听者:一般用于当前脚本自身
RemoveAllEventListener(listener) object listener Clean up all events from the current listener

清理当前监听者 所有的事件
RemoveEventListener(eventType, listener) 1. string 事件类型

2.object 监听者
clean up the current listener specified type event

清理当前监听者 指定类型 事件
DispatchEvent(eventType, object data = null) 1.string 事件类型

2.object 数据包
packets, which can be of any type,

For example: a string, an int, a dictionary, a list, a class, table etc

数据包,可以是任何类型,

比如:一个字符串、一个int、字典、列表、类

1.luaEvent、CS.GMS.LuaEventDispatcher.Instance,全局工作

--lua--
function Start()
  luac=  transform:GetComponent("LuaComponent")
  Log(luac.name.." ".."..test")
  luaEvent:AddEventListener("game.end",this,onGameEnd) --监听事件
end

function testEnd()
    luaEvent:DispatchEvent("game.end");     -- 分发事件
end

function onGameEnd(info)
    log('onGameEnd')
     SceneManager.LoadSceneAsync("GameDemoEnter");
end

function OnDestroy()
--清理当前监听着(this) 指定类型 事件
    luaEvent:RemoveEventListener("game.end",this)
 --清理当前监听着(this)所有的事件
    luaEvent:RemoveAllEventListener(this)
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

--带参数:event with parameters

function Start()
    luaEvent:AddEventListener("game.test1",this, OnTest1)
    luaEvent:DispatchEvent("game.test1",123)
    --or
    luaEvent:AddEventListener("game.test2",this, OnTest2)
    local test ={}
    test['id']=564
    luaEvent:DispatchEvent("game.test2",test)
end

function OnDestroy()
   --清理当前监听着(this) 指定类型 事件
   luaEvent:RemoveEventListener("game.test1",this)
   luaEvent:RemoveEventListener("game.test2",this)
    --or
    --清理当前监听着(this)所有的事件
   luaEvent:RemoveAllEventListener(this);
end

function OnTest1(info)
   if info:CheckData() then
        local data = info.Data
        print(data)    --123 
   end     
end

function OnTest2(info)
    if info:CheckData() then
        local data = info.Data
        print(data['id']) --564
    end
end    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

2.c# 中 LuaEventDispatcher.Instance

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WoogiWorld.Event;

public class VMSocket : MonoBehaviour
{
    private void Awake()
    {
        AddEvent();
        LuaEventDispatcher.Instance.AddEventListener("game.open_vmTest",this, OnTest);
    }

    private void OnDestroy()
    {
        //清理当前监听着(this) 指定类型 事件
        LuaEventDispatcher.Instance.RemoveEventListener("game.open_vmTest",this);
        //清理当前监听着(this)所有的事件
        LuaEventDispatcher.Instance.RemoveAllEventListener(this);
    }

    public void OnTest(EventMessage info)
    {
        if (info.CheckData())
        {
            if ((bool)info.Data)
            {
               Debug.Log("i am ture");
            }
            else
            {
               Debug.Log("i am false");
            }
        }
    }   
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Last Updated: Thu, 11 Apr 2024 06:54:22 GMT